import { Base64 } from 'js-base64';
import { AnyJson } from '@services/data-types/common.type';


export function codeJson(source: any, type = 'encode'): AnyJson {
  const result = {};
  for (const attr in source) {
    if (source.hasOwnProperty(attr)) {
      result[Base64[type](attr)] = Base64[type][source](attr);
    }
  }
  return result;
}

function deleteCookie(name) {
  document.cookie = name + '=; expires=Thu, 01 Jan 2020 00:00:00 GMT; path=/';
}

export function deleteAllCookies() {
  const cookies = document.cookie.split(';');
  cookies.forEach(cookie => {
    const eqPos = cookie.indexOf('=');
    const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    deleteCookie(name);
  });
}
